﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;

namespace SharpGeo.IO.WellKnown
{    
    /// <summary>
    /// Represents a Data Value within a WellKnownGraph
    /// </summary>
    public class WellKnownData : WellKnownGraph
    {
        #region Constructor
        /// <summary>
        /// Constructs an new WellKnownGraphDataType with a passed value
        /// </summary>
        /// <param name="value">The value to assign to this WellKnownGraphDataType</param>
        public WellKnownData(object value)
        {
            Value = value;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the value associated with this WellKnownGraphDataValue
        /// </summary>
        public object Value { get; set; }

        /// <summary>
        /// Gets the Keyword of this WellKnownGraphDataValue (WellKnownKeyword.Value)
        /// </summary>
        public override WellKnownKeyword Keyword
        {
            get { return WellKnownKeyword.Value; }
            set { }
        }

        /// <summary>
        /// Checks if the value is quoted
        /// </summary>
        public bool IsQuoted
        {
            get
            {
                if (Value == null)
                {
                    return false;
                }

                var v = Value.ToString();

                return (
                    v.StartsWith("\"", StringComparison.OrdinalIgnoreCase) &&
                    v.EndsWith("\"", StringComparison.OrdinalIgnoreCase));
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Overrides WellKnownGraph.AddChild and does nothing
        /// </summary>
        /// <param name="child">The child to add (ignored)</param>
        public override void AddChild(WellKnownGraph child)
        {
            //do nothing - this should never happen
        }

        /// <summary>
        /// Overrides WellKnownGraph.AddValue and does nothing
        /// </summary>
        /// <param name="value">The value to add (ignored)</param>
        public override void AddValue(object value)
        {
            //do nothing - this should never happen                
        }

        /// <summary>
        /// Tries to intelligently parse the value
        /// </summary>
        /// <returns>Returns the object value or null if this fails</returns>
        public object TryParseValue()
        {
            if (Value == null)
            {
                return null;
            }

            var v = Value.ToString();

            //if this is quoted assume it is a string
            if (v.StartsWith ("\"", StringComparison.OrdinalIgnoreCase) &&
                v.EndsWith("\"", StringComparison.OrdinalIgnoreCase))
            {
                //remove the quotes
                return v.Substring(1, v.Length-1);
            }

            var values = v.Split(' ');

            //if we only get one value .. assume it is a double
            if (values.Length == 1)
            {
                double r;
                var ret = double.TryParse(values[0], out r);

                if (ret)
                    return r;
                else
                    return null;
            }

            //otherwise assume it is a coordinate
            var ordinates = new double[values.Length];

            for (int i = 0; i < values.Length; i++)
            {
                double r;
                
                var ret = double.TryParse(values[i], out r);

                if (!ret)
                    return null;

                ordinates[i] = r;
            }

            return new Coordinate(ordinates);
        }
        #endregion
    }
}
